package com.itheima.test7;

import java.util.stream.StreamSupport;

public class SudentTest {
    public static void main(String[] args) {
        Student[] sts = new Student[3];

        sts[0] = new Student(1, "小明", 18);
        sts[1] = new Student(2, "小张", 19);
        sts[2] = new Student(3, "小红", 20);

        Student student = new Student(4, "小李", 21);

        boolean flag = containes(sts, student);
        int stsNumber = 5;
        if (!flag) {
            int count = getCount(sts);
            if (count == sts.length) {
                Student[] newArr = createNewArr(sts);
                newArr[count] = student;
                printArr(newArr);
                System.out.println("+++++++++++++++++");
                int index = getIndex(newArr, stsNumber);
                judgeIndex(index, newArr);
                printArr(newArr);
            } else {
                sts[count] = student;
                printArr(sts);
                System.out.println("+++++++++++++++++");
                int index = getIndex(sts, stsNumber);
                judgeIndex(index, sts);
                printArr(sts);
            }
        } else {
            System.out.println("学生已经存在，无需添加了");
        }
    }

    public static void judgeIndex(int index, Student[] arr) {
        if(index >= 0) {
            arr[index] = null;
        } else {
            System.out.println("不存在该学生，删除失败！");
        }
    }

    public static int getIndex(Student[] arr, int stsNumber) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null && arr[i].getStNumber() == stsNumber) {
                return i;
            }
        }
        return -1;
    }

    public static void printArr(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null) {
                System.out.println(arr[i].getStNumber() + ", " + arr[i].getName() + ", " + arr[i].getAge());
            }
        }
    }

    public static Student[] createNewArr(Student[] sts) {
        Student[] newArr = new Student[sts.length + 1];
        for (int i = 0; i < sts.length; i++) {
            newArr[i] = sts[i];
        }
        return newArr;
    }

    public static int getCount(Student[] sts) {
        int count = 0;
        for (int i = 0; i < sts.length; i++) {
            if (sts[i] != null) {
                count++;
            }
        }
        return count;
    }

    public static boolean containes(Student[] sts, Student student) {
        for (int i = 0; i < sts.length; i++) {
            if (sts[i] != null && sts[i].getStNumber() == student.getStNumber()) {
                return true;
            }
        }
        return false;
    }
}
